package com.webull.information.center.carwler.common.util.news;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.framework.util.UtilDate;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;

/**
 * 新浪个股新闻资讯列表解析工具
 * 
 * @author shimingjun
 * @date 2016年8月26日 下午3:51:37
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class BloombergNewsListUitl {
	private @Autowired
    HttpJsoupClient htmlPrase;
	protected final Logger logger = LogManager.getLogger(getClass());

	/*
	 * 获取CNBC个股资讯首页对象并解析新闻条目列表
	 */
	public List<BloomergItem> parseNewsList(String url) {// url="https://www.bloomberg.com/quote/AAPL:US"
		Document doc = htmlPrase.parseDocumentFromUrl(url, true);
		if (doc == null) {
			logger.warn("BloombergNewsListUitl parseNewsList parse url:{} ,is null!!", url);
			return null;
		}
		return parseNewsListUS(doc);
	}

	private List<BloomergItem> parseNewsListUS(Document doc) {
		List<BloomergItem> result = new ArrayList<>();

		// 美股新闻页面
		Optional.ofNullable(doc.select("div[data-group=company-news]").first())//
				.map(v -> v.getElementsByTag("article"))//
				.ifPresent(v -> {
					v.forEach(v0 -> {
						try {
							BloomergItem item = new BloomergItem();
							Date newsTime = Optional.ofNullable(v0.getElementsByTag("time").first())
									.map(v2 -> StringUtils.stripToNull(v2.attr("datetime")))//
									.map(v2 -> UtilDate.parse(v2, 0, "yyyy-MM-dd'T'HH:mm:ss")).orElse(null);
							item.setNewsTime(newsTime);
							Optional.ofNullable(v0.select("a.news-story__url").first()).ifPresent(v2 -> {
								item.setLink(v2.absUrl("href"));
								if (StringUtils.containsIgnoreCase(item.getLink(), "investopedia.com")) {// 精简url
									item.setLink(StringUtils.substringBeforeLast(item.getLink(), "?"));
								}
								item.setTitle(v2.text());
							});
							Optional.ofNullable(v0.select("span.news-story__provider").first()).ifPresent(v2 -> {
								item.setSource(StringUtils.removePattern(v2.text(), "^(-|\\s)*"));
							});
							if (StringUtils.isEmpty(item.getSource())) {
								item.setSource("bloomberg.com");
							}
							if (item.getNewsTime() == null)
								item.setNewsTime(new Date());
							result.add(item);
						} catch (Exception e) {
							logger.warn("BloombergNewsListUitl error:{}", e);
						}
					});
				});

		return result;
	}

	public static void main(String[] args) throws IOException {
		System.out.println(StringUtils.substringBeforeLast("2sdfkhsdkfhsdkhfsf1", "?"));
		String url2 = "https://www.bloomberg.com/quote/AAPL:US";

		// Connection connection = Jsoup.connect(url2)
		// Connection connection = Jsoup.connect(urlHK)
		Connection connection = Jsoup.connect(url2).userAgent(
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
				// .header("x-client-data",
				// "CIq2yQEIpbbJAQjEtskBCP2VygEIwpjKAQjwnMoB")
				.header("x-client-data", RandomStringUtils.randomAlphanumeric(40));
//		connection.proxy("10.1.2.174", 3088);
// 		connection.proxy("127.0.0.1", 1080);


		SocketAddress address = new InetSocketAddress("127.0.0.1", 1080);
		connection.proxy(new Proxy(Proxy.Type.SOCKS,address) ); // 设置代理

		org.jsoup.nodes.Document doc = connection.timeout(10000).get();

		// List<CNBCItem> items = new
		// BloombergNewsListUitl().parseNewsListHK(doc);
		List<BloomergItem> items = new BloombergNewsListUitl().parseNewsListUS(doc);
		for (int i = 0; i < items.size(); i++) {
			System.out.println(items.get(i).getLink());
		}
		System.out.println(items);
	}

	public static class BloomergItem {
		private Date newsTime;

		private String title;
		private String link;
		private String source;

		public Date getNewsTime() {
			return newsTime;
		}

		public void setNewsTime(Date newsTime) {
			this.newsTime = newsTime;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getLink() {
			return link;
		}

		public void setLink(String link) {
			this.link = link;
		}

		public String getSource() {
			return source;
		}

		public void setSource(String source) {
			this.source = source;
		}

		@Override
		public String toString() {
			return "BloomergItem [newsTime=" + newsTime + ", source=" + source + ", title=" + title + ", link=" + link
					+ "]";
		}

	}

}
